package com.xuhf.basic;

import com.xuhf.config.Config;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.Random;

public class Producer1 {

    public static void main(String[] args) throws Exception {
        //1 创建一个生产者对象, 并且指定一个生产者组
        DefaultMQProducer producer = new DefaultMQProducer(Config.PRODUCER_GROUP);
        //2 设置名字服务的地址
        producer.setNamesrvAddr(Config.NAMESRV_ADDR);
        //3 启动生产者
        producer.start();
        for (int i = 0; i < 1000; i++) {
            //4 创建一个消息
            String msg = "hello,rocketmq---" + String.format("%05d", i);
            Message message = new Message(Config.TOPIC, Config.TAG1, msg.getBytes("utf-8"));
            //5 发送消息
            SendResult sendResult = producer.send(message);
            System.out.println("生产者发送消息，消息ID：" + sendResult.getMsgId() + "，发送状态：" + sendResult.getSendStatus());
            Random random = new Random();
            int sec = random.nextInt(2000) + 200;
            Thread.sleep(sec);
        }

        //6 关闭连接
        producer.shutdown();
    }
}
